﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LogicalComponent.util;
using LogicalComponent.util.exception;

namespace LogicalComponent.userInterface
{

    /// <summary>
    /// Class that helps with handling mouse clicks in the grid.
    /// </summary>
    public class ClickUtil
    {

        // *************************************************************
        // ************** CLASSES **************************************
        // *************************************************************


        /// <summary>
        /// Grouping of data used to map a click to a grid coordinate
        /// </summary>
        public class ClickMap
        {

            /// <summary>
            /// Gets the origin X.
            /// </summary>
            public float OriginX { get; private set; }

            /// <summary>
            /// Gets the origin Z.
            /// </summary>
            public float OriginZ { get; private set; }

            /// <summary>
            /// Gets the tile size X.
            /// </summary>
            public float TileSizeX { get; private set; }

            /// <summary>
            /// Gets the tile size Z.
            /// </summary>
            public float TileSizeZ { get; private set; }

            /// <summary>
            /// Initializes a new instance of the <see cref="ClickMap"/> class.
            /// </summary>
            /// <param name="originX">The origin X.</param>
            /// <param name="originZ">The origin Z.</param>
            /// <param name="tileSizeX">The tile size X.</param>
            /// <param name="tileSizeZ">The tile size Z.</param>
            public ClickMap(float originX, float originZ, float tileSizeX, float tileSizeZ)
            {
                if (tileSizeX <= 0 || tileSizeZ <= 0)
                {
                    throw new LogicalUserError(
                        "Invalid arguments for ClickUtil.ClickMap, tileSize must be greater than 0");
                }
                this.OriginX = originX;
                this.OriginZ = originZ;
                this.TileSizeX = tileSizeX;
                this.TileSizeZ = tileSizeZ;
            }

            public ClickMap(float originX, float originZ, float tileSize) :
                this(originX, originZ, tileSize, tileSize) { }
                
        }

        // *************************************************************
        // ************** FUNCTIONS ************************************
        // *************************************************************

        /// <summary>
        /// Gets the click coordinate.
        /// </summary>
        /// <param name="clickX">The click X.</param>
        /// <param name="clickZ">The click Z.</param>
        /// <param name="map">The map.</param>
        /// <returns></returns>
        public static Coordinate GetClickCoordinate(float clickX, float clickZ, ClickMap map)
        {
            float xNormalized = clickX - map.OriginX;
            float zNormalized = clickZ - map.OriginZ;
            float x = xNormalized / map.TileSizeX;
            float z = zNormalized / map.TileSizeZ;
            if (xNormalized < 0)
            {
                x -= 1;
            }
            if (zNormalized < 0)
            {
                z -= 1;
            }
            return new Coordinate((int)x, (int)z);
        }

        public class HexClickMap
        {
            public static Coordinate GetClickCoordinate(float clickX, float clickZ)
            {
                splitRecX = Math.Floor(clickX / this.Width);
                splitRecZ = Math.Floor(clickZ / (this.Height / 2));
                leftCoordinateX = splitRecX;
                leftCoordinateZ = Math.Floor((splitRecZ - splitRecX + 1) / 2);
                rightCoordinateX = splitRecX + 1;
                rightCoordinateZ = Math.Floor((splitRecZ - splitRecX) / 2);

            }

            private int getXCoordinate(float clickX, float clickZ)
            {
                float mod = clickZ % this.Height;
                if (mod > this.Height / 2)
                {
                    mod = this.Height - mod;
                }
                
                float offsetRatio = mod / (this.Height / 2);
                float offset = offsetRatio * this.SideExtra;
                float x = clickX + offset - this.Width / 2;
                
            }
        }
    }
}
